package com.zjdiepu.www.task.insurance;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.zjdiepu.www.base.enums.status.InsuranceStatus;
import com.zjdiepu.www.base.enums.type.SysParamsType;
import com.zjdiepu.www.common.util.DateUtil;
import com.zjdiepu.www.commpent.RedisObjectClient;
import com.zjdiepu.www.ibiz.dh.IDhArchiveService;
import com.zjdiepu.www.ibiz.dh.IDhInsuranceRenewalService;
import com.zjdiepu.www.ibiz.dh.IDhInsuranceService;
import com.zjdiepu.www.ibiz.dh.IFkInsureListService;
import com.zjdiepu.www.ibiz.system.IBaseSysParamsService;
import com.zjdiepu.www.ibiz.yw.IYwBusinessCarService;
import com.zjdiepu.www.ibiz.yw.IYwBusinessService;
import com.zjdiepu.www.model.dh.dto.DhArchive;
import com.zjdiepu.www.model.dh.dto.DhInsurance;
import com.zjdiepu.www.model.dh.dto.DhInsuranceRenewal;
import com.zjdiepu.www.model.dh.ext.DhArchiveExt;
import com.zjdiepu.www.model.dh.ext.DhInsuranceExt;
import com.zjdiepu.www.model.yw.dto.YwBusinessCar;
import com.zjdiepu.www.model.yw.ext.YwBusinessCarExt;
import com.zjdiepu.www.model.yw.response.YwBusinessDetail;
/**
 * 项目名称：diepu-parent-task <br/> 
 * 类名称：InsuranceTask <br/>  
 * 类描述：续保生成 <br/>    
 * 创建人：xiongxiaotun <br/> 
 * 创建时间：2017年6月2日 下午4:18:07  <br/>   
 * 修改人：xiongxiaotun <br/> 
 * 修改时间：2017年6月2日 下午4:18:07   <br/>  
 * 修改备注：    <br/> 
 * @version V1.0   <br/>
 */
@Component
public class InsuranceTask {
	private Logger logger = LoggerFactory.getLogger(getClass());
	@Resource
	private IYwBusinessService ywBusinessService;
	@Resource
	private IYwBusinessCarService ywBusinessCarService;
	@Resource
	private IDhInsuranceService dhInsuranceService;
	@Resource
	private IBaseSysParamsService baseSysParamsService;
	@Resource
	private IDhInsuranceRenewalService dhInsuranceRenewalService;
	@Resource
	private IFkInsureListService fkInsureListService;
	@Resource
	private RedisObjectClient redisObjectClient;
	@Autowired
	private IDhArchiveService dhArchiveService;
	
	/**
	 * @author xiongxiaotun <br/>
	 * @Title: insuranceContinue  <br/>
	 * @Description: 续保生成 <br/>
	 * @version V1.0  <br/>
	 */
	@Scheduled(cron="${schedule.insurance.handler.cron}")
	public void insuranceContinue() {
		logger.debug("insurance create start.");
		DhInsuranceExt dhInsuranceExt = new DhInsuranceExt();
		dhInsuranceExt.setBaseWhere();
		Integer paramInteger = (Integer)SysParamsType.XU_BAO_TI_XING.getDefaultValue();
		String paramValue = baseSysParamsService.getValueByCode(SysParamsType.XU_BAO_TI_XING.getCode());
		if(StringUtils.isNotBlank(paramValue)) {
			paramInteger = Integer.valueOf(paramValue);
		}
		Date lastTime = (Date)redisObjectClient.get("last_time");
		if(lastTime != null) {
			dhInsuranceExt.setDateEndStart(DateUtil.truncAndAddDays(lastTime, 0)); //>=上次结束时间
		}
		Date now = new Date();
		Date endDate = DateUtil.truncAndAddDays(now, paramInteger - 1);
		dhInsuranceExt.setDateEndEnd(endDate);
		
		boolean continueHandle = true;
		int page = 0;
		final int row = 50;
		dhInsuranceExt.setRows(row);
		while(continueHandle){
			dhInsuranceExt.setPage(++page); //从第一页开始
			List<DhInsurance> dhInsurances = dhInsuranceService.selectByExt(dhInsuranceExt);
			List<DhInsuranceRenewal> dhInsuranceRenewals = new ArrayList<DhInsuranceRenewal>();
			for(DhInsurance dhInsurance : dhInsurances) {
				DhInsuranceRenewal dhInsuranceRenewal = new DhInsuranceRenewal();
				dhInsuranceRenewal.initInsertData("schedule", "schedule");
				dhInsuranceRenewal.setDateDue(dhInsurance.getDateEnd());
				dhInsuranceRenewal.setNameCompany(dhInsurance.getCompanyInsurance());
				dhInsuranceRenewal.setYear(dhInsurance.getYear() + 1);
				String busiId = dhInsurance.getBusiId();
				YwBusinessDetail ywBusinessDetail = ywBusinessService.getMultiById(busiId);
				if(ywBusinessDetail == null) {
					continue;
				}
				dhInsuranceRenewal.setBusiId(busiId);
				dhInsuranceRenewal.setCusId(ywBusinessDetail.getCustomerId());
				dhInsuranceRenewal.setNameSalesman(ywBusinessDetail.getSalesmanName());
				dhInsuranceRenewal.setNameCus(ywBusinessDetail.getCustomerName());
				dhInsuranceRenewal.setIdnoCus(ywBusinessDetail.getCustomerNo());
				
				DhArchiveExt dhArchiveExt = new DhArchiveExt();
				dhArchiveExt.setCusId(ywBusinessDetail.getCustomerId());
				dhArchiveExt.setBusiId(busiId);
				dhArchiveExt.setRows(1);
				List<DhArchive> dhArchives = dhArchiveService.selectByExt(dhArchiveExt);	
				if(CollectionUtils.isNotEmpty(dhArchives)) {
					DhArchive dhArchive = dhArchives.get(0);
					dhInsuranceRenewal.setNoRecord(dhArchive.getNoRecord());
				}
				
				YwBusinessCarExt ywBusinessCarExt = new YwBusinessCarExt();
				ywBusinessCarExt.setBaseWhere();
				ywBusinessCarExt.setRows(1);
				ywBusinessCarExt.setBusiId(busiId);
				List<YwBusinessCar> ywBusinessCars = ywBusinessCarService.selectByExt(ywBusinessCarExt);
				if(CollectionUtils.isNotEmpty(ywBusinessCars)) {
					YwBusinessCar ywBusinessCar = ywBusinessCars.get(0);
					dhInsuranceRenewal.setNameCar(ywBusinessCar.getNameCarModel());
					dhInsuranceRenewal.setPrice(ywBusinessCar.getPrice());
				}
				dhInsuranceRenewal.setTypeRenewal("01");
				dhInsuranceRenewal.setStatusRenewal(InsuranceStatus.DAI_BAO.getCode());
				dhInsuranceRenewal.setOperatorId(null);
				dhInsuranceRenewal.setOperatorName(null);
				dhInsuranceRenewal.setAcceptDate(null);
				dhInsuranceRenewals.add(dhInsuranceRenewal);
			}
			
			if(CollectionUtils.isNotEmpty(dhInsuranceRenewals)) {
				dhInsuranceRenewalService.insertBatch(dhInsuranceRenewals);
			}
			
			continueHandle = (dhInsuranceRenewals.size() == row);
		}
		redisObjectClient.put("last_time", endDate);
		
		
		logger.debug("insurance create end.");
	}
}
